﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using hethongbansach.Models;
using System.Text.RegularExpressions;
using saigonbook.Dal;
using saigonbook.Biz;
namespace hethongbansach.Controllers
{
    public class cartController : Controller
    {
        public RedirectToRouteResult giohangthem(string masach, int soluong = 1)
        {
            List<BIZ_sach> listsp = new List<BIZ_sach>();
            listsp = BIZ_sach.Select_sach();
            foreach (BIZ_sach item in listsp)
            {
                //BIZ_tonkho tk = new BIZ_tonkho();
                //tk.ngay = DateTime.Now.Date;
                //tk.sach = new BIZ_sach();
                //tk.sach.masach = item.masach;
                //int _slcon = tk.soluongtonkho(tk);
                if (item.masach.ToString().Equals(masach))
                    GetCart().AddItem(item.masach.ToString(), item.tensach, (int)item.giaban, soluong);
            }
            return RedirectToAction("Index");
        }

        public RedirectToRouteResult xoasanpham(string masanpham)
        {
            GetCart().RemoveLine(masanpham);
            return RedirectToAction("Index");
        }

        public bool IsNumber(string pText)
        {
            Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
            return regex.IsMatch(pText);
        }

        public RedirectToRouteResult capnhatsoluong(string masanpham, string soluong)
        {
            int soluongton = 0;
            DateTime _ngay = DateTime.Now;
            int _masach = int.Parse(masanpham.ToString());
            BIZ_tonkho tk = new BIZ_tonkho();
            tk.ngay = DateTime.Now.Date;
            tk.sach = new BIZ_sach();
            tk.sach.masach = int.Parse(masanpham.ToString());
            soluongton = tk.soluongtonkho(tk);
            if (IsNumber(soluong))
            {
                if (int.Parse(soluong) <= soluongton)
                {
                    if (int.Parse(soluong) < 1)
                        GetCart().capnhatsoluong(masanpham, 1);
                    else
                        GetCart().capnhatsoluong(masanpham, int.Parse(soluong));
                }
                else { GetCart().capnhatsoluong(masanpham, soluongton); }
            }
            else
            {
                GetCart().capnhatsoluong(masanpham, 1);
            }
            return RedirectToAction("Index");
        }

        public RedirectToRouteResult xoahet()
        {
            GetCart().Clear();
            return RedirectToAction("Index");
        }

        [ChildActionOnly]
        public ActionResult menugiohang()
        {
            return PartialView(new CartIndexViewModel { Cart = GetCart() });
        }

        private Cart GetCart()
        {
            Cart cart = (Cart)Session["Cart"];
            if (cart == null)
            {
                cart = new Cart();
                Session["Cart"] = cart;
            }
            return cart;
        }

        [ChildActionOnly]
        public ActionResult footergiohang()
        {
            return PartialView();
        }
        public ActionResult thanhtoangiohang()
        {
            if (GetCart().Lines.Count() > 0)
            {
                return View(new CartIndexViewModel
                {
                    Cart = GetCart()
                });
            }
            return RedirectToAction("index");
        }

        Cart cart = new Cart();

        public RedirectToRouteResult save(FormCollection f, string tongtien)
        {
            BIZ_donhang hd = new BIZ_donhang();
            hd.user = new BIZ_user();
            hd.user.username=(string)Session["User"];
            hd.madonhang = hd.laymadonhang();
            hd.diachigiaohang = hd.user.Thongtin_user(hd.user.username).diachi;
            hd.tongtien = int.Parse(tongtien);
            hd.ngaymuahang = DateTime.Now.Date;
            hd.ngaygiaohang = DateTime.Now.Date;
            hd.tinhtrang = "chưa liên lạc";
            GetCart().save(hd);
            GetCart().Clear();
            return RedirectToAction("trangchu", "home");
        }
        public ViewResult Index()
        {
            return View(new CartIndexViewModel
            {
                Cart = GetCart()
            });
        }
    }
}
